package com.hcg.multidatasourceofanno2.common.utils;

import org.springframework.core.env.Environment;

import java.io.PrintWriter;
import java.io.StringWriter;

public class StringUtils extends org.apache.commons.lang3.StringUtils {
    public StringUtils() {}

    public static String getDefaultIfEmpty(String str, String defaultValueStr)
    {
        if ((null == str) || ("".equals(str))) {
            return defaultValueStr;
        }
        return str;
    }

    public static String toString(Object obj)
    {
        if (null == obj) {
            return null;
        }
        return String.valueOf(obj);
    }

    public static String leftPadIfNecessary(String value, int length, char padChar) {
        if (-1 == length) {
            return value;
        }
        return leftPad(value, length, padChar);
    }

    public static String rightPadIfNecessary(String value, int length, char padChar)
    {
        if (-1 == length) {
            return value;
        }
        return rightPad(value, length, padChar);
    }

    public static String removeLeftPad(String value, char padChar)
    {
        if (isEmpty(value)) {
            return value;
        }
        int index = 0;
        char[] valueChars = value.toCharArray();
        for (int i = 0; i < valueChars.length; i++) {
            if (valueChars[i] != padChar) {
                index = i;
                break;
            }
        }
        return value.substring(index);
    }

    public static String removeRightPad(String value, char padChar)
    {
        if (isEmpty(value)) {
            return value;
        }
        int index = value.length() - 1;
        char[] valueChars = value.toCharArray();
        for (int i = value.length() - 1; i >= 0; i--) {
            if (valueChars[i] != padChar) {
                index = i + 1;
                break;
            }
        }
        return value.substring(0, index);
    }

    public static String toString(Throwable e)
    {
        StringWriter w = new StringWriter();
        PrintWriter p = new PrintWriter(w);
        p.print(e.getClass().getName() + ": ");
        if (e.getMessage() != null) {
            p.print(e.getMessage() + "\n");
        }
        p.println();
        try {
            e.printStackTrace(p);
            return w.toString();
        } finally {
            p.close();
        }
    }

    public static String parsePlaceHolder(String originalStr, Environment environment)
    {
        if (JudgeUtils.isBlank(originalStr)) {
            return originalStr;
        }
        StringBuilder sb = new StringBuilder();
        String str = originalStr;
        for (;;) {
            int start = str.indexOf("${");
            if (-1 == start) {
                break;
            }
            sb.append(str.substring(0, start));
            int end = str.indexOf("}");
            sb.append(environment.getProperty(str.substring(start + 2, end)));
            sb.append(str.substring(end + 1));
            str = sb.toString();
            sb.delete(0, sb.length());
        }
        return str;
    }
}